<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Constants\CrowSendingConstants;
use App\CustHub\Model\ChCrowdSending;
use App\CustHub\Model\ChCrowdSendingLoop;
use Carbon\Carbon;
use Hyperf\Config\Annotation\Value;
use Hyperf\Cache\Annotation\Cacheable;
use Mine\Abstracts\AbstractMapper;


class ChSendingMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChCrowdSending::class;
    }

    //#[Cacheable(prefix: "sending_calendar", value:"#{uid}_#{date}#{task_type}#{status}", ttl:3600)]
    public function getTaskList($uid,$date,$task_type=null,$status=null)
    {
        $carbonDate = Carbon::parse($date);
        $list = getDatesOfMonth($carbonDate->year,$carbonDate->month);
        $newList=[];
        $where['uid']=$uid;
        if(!empty($task_type)){
            $where['crowd_type']=$task_type;
        }
        $sendingLoopList=ChCrowdSendingLoop::query()->where($where)->where('status',1)->get()->toArray();
        $sendingCommonList=ChCrowdSending::query()->where($where)
            ->whereRaw("DATE_FORMAT(start_time, '%Y-%m') = '$date'")
            ->select(['id','title','status','start_time','crowd_type as task_type'])
            ->get()
            ->toArray();
        if(filled($status)){
            $where['status']=$status;
        }
        foreach ($list as $day){
            $sendingList=[];
            //查询循环任务
            if($status==CrowSendingConstants::WAIT_SEND || !filled($status)){
                foreach ($sendingLoopList as $sending){
                    $timesConf=$sending['send_times_conf'];
                    switch ($sending['send_times']){
                        case 3:
                            //每天
                            $sendTime = strtotime($day." ".$timesConf['time']);
                            if((!$sending['is_loop_end'] || strtotime($day) < strtotime($sending['loop_end_time'])) && $sendTime > time()){
                                $sendingList[]=[
                                    'id'=>$sending['id'],
                                    'task_type'=>$sending['crowd_type'],
                                    'title'=>$sending['title'],
                                    'status'=>0,
                                    'start_time'=>$day." ".$timesConf['time'],
                                    'no_data'=>true
                                ];
                            }
                            break;
                        case 4:
                            //本周
                            $week = date("w",strtotime($day));
                            $sendTime = strtotime($day." ".$timesConf['time']);
                            if((!$sending['is_loop_end'] || strtotime($day) < strtotime($sending['loop_end_time'])) && in_array($week,$timesConf['day']) && $sendTime > time()){
                                $sendingList[]=[
                                    'id'=>$sending['id'],
                                    'title'=>$sending['title'],
                                    'task_type'=>$sending['crowd_type'],
                                    'status'=>0,
                                    'start_time'=>$day." ".$timesConf['time'],
                                    'no_data'=>true
                                ];
                            }
                            break;
                        case 5:
                            //本月
                            $d = date("d",strtotime($day));
                            $sendTime = strtotime($day." ".$timesConf['time']);
                            if((!$sending['is_loop_end'] || strtotime($day) < strtotime($sending['loop_end_time'])) && in_array($d,$timesConf['day']) && $sendTime > time()){
                                $sendingList[]=[
                                    'id'=>$sending['id'],
                                    'title'=>$sending['title'],
                                    'task_type'=>$sending['crowd_type'],
                                    'status'=>0,
                                    'start_time'=>$day." ".$timesConf['time'],
                                    'no_data'=>true
                                ];
                            }
                            break;
                    }
                }
            }

            !empty($sendingList) && $newList[$day]=$sendingList;
        }
        //查询及时任务和单次定时任务
        foreach ($sendingCommonList as $sending){
            $startDate=datetime(strtotime($sending['start_time']),'Y-m-d');

            if (in_array($startDate, $list)) {
                $newList[$startDate][] = [
                    'id'=>$sending['id'],
                    'task_type'=>$sending['task_type'],
                    'title'=>$sending['title'],
                    'status'=>$sending['status'],
                    'start_time'=>$sending['start_time']
                ];
            }
        }
        return $newList;
    }

}